package leetcode.editor.cn1;

public class XuanZhuanShuZuDeZuiXiaoShuZiLcof_3 {

    public static void main(String[] args) {
        Solution solution = new XuanZhuanShuZuDeZuiXiaoShuZiLcof_3().new Solution();

        System.out.println(solution.minArray(new int[]{10, 1, 10, 10, 10}));
        System.out.println(solution.minArray(new int[]{3, 4, 5, 1, 2}));
        System.out.println(solution.minArray(new int[]{2, 2, 2, 0, 1}));
        System.out.println(solution.minArray(new int[]{3, 1, 1}));
        System.out.println(solution.minArray(new int[]{1, 1, 3, 1}));
    }

    class Solution {
        public int minArray(int[] numbers) {
            if (numbers.length == 0)
                return -1;
            if (numbers.length == 1)
                return numbers[0];
            int low = 0, hight = numbers.length - 1, mid = -1;
            while (low < hight) {
                mid = (low + hight) / 2;
                if (numbers[mid] > numbers[hight]) {
                    // 说明最小值在 (mid, hight]中间
                    low = mid + 1;
                } else if (numbers[mid] < numbers[hight]) {
                    // 说明最小值在[low, mid]中间
                    hight = mid;
                } else {
                    // 忽略最高值
                    hight = hight - 1;
                }
            }
            return numbers[low];
        }
    }
}
